//
//  QZShopInfoController.m
//  WeiWeiHaircut
//
//  Created by apple on 15/12/9.
//  Copyright © 2015年 超群. All rights reserved.
//

#import "QZShopInfoController.h"
#import "QZShopInfoHeaderView.h"
#import "QZShopInfoModel.h"

#import "QZInfoLabelCell.h"
#import "NSString+size.h"
@interface QZShopInfoController ()

@property (nonatomic, strong) QZShopInfoModel *shopInfoModel;

@end

@implementation QZShopInfoController{
    
    QZShopInfoHeaderView *_headerView;
    QZInfoLabelCell *_footView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
 
    //设置NavigationBar和创建tabLeView
    [self createTableView];
    
    
    [self downloadData];
}

-(void)createTableView{
    
    
    [self addBtnOnNavWithTitle:@"返回" andImageName:@"返回" andTarget:self andAction:@selector(backAction) andFrame:CGRectMake(0, 0, 45, 20) andDirection:left];
    
    self.navigationItem.title = @"商品详情";
    
    [self createTableViewWithPlainStyle:NO andSeparatorStyleNone:NO andBackGroudImageName:nil];
    self.tableView.bounces = NO;
    _headerView = [[NSBundle mainBundle] loadNibNamed:@"QZShopInfoHeaderView" owner:nil options:nil].lastObject;
    
    _headerView.frame = CGRectMake(0, 0, WIDTH, .8*WIDTH);
 
    self.tableView.tableHeaderView = _headerView;
    
    _footView = [[NSBundle mainBundle]loadNibNamed:@"QZInfoLabelCell" owner:nil options:nil].lastObject;
    
    self.tableView.tableFooterView = _footView;
    
    
    
    UIButton *addShopCar =[UIButton buttonWithType:UIButtonTypeCustom];
    
    addShopCar.frame = CGRectMake(0, HEIGHT-64-55, WIDTH, 55);

    addShopCar.titleLabel.text = @"加入购物车";
    
    [addShopCar setTitle:@"加入购物车" forState:UIControlStateNormal];
    
    [addShopCar setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [addShopCar addTarget:self action:@selector(addShopCarClick:) forControlEvents:UIControlEventTouchUpInside];
    addShopCar.backgroundColor = purColor2;
    [self.view addSubview:addShopCar];

    
}
#pragma mark 添加购物车
-(void)addShopCarClick:(UIButton *)btn{
    
    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"Details.GetDetailsAdd"];
    
    NSDictionary *dic = @{
                          @"user_id":@([[QZUserInfo userId] integerValue]),
                          @"prod_id": @(_shopInfoModel.prodId)
                          };
    
    [self.request QZRequest_POST:url parameters:dic tagNSString:@"添加购物车" stopRequest:YES isSerializer:NO isCache:NO];
    
}

-(void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
    
}


-(void)request_POST_FinishValue:(id)value tagNSString:(NSString *)tag{

    if ([tag isEqualToString:@"商品详情"]) {
        
        _shopInfoModel = [[QZShopInfoModel alloc] initWithDictionary:value[@"data"][@"data"] error:nil];
        
        _headerView.shopInfoModel =_shopInfoModel;
        
        _footView.prodInfo = _shopInfoModel.prodInfo;
        
        
        //设置Label的高度
        CGFloat label_H = HEIGHT - _headerView.frame.size.height -64;
        CGFloat label_Height =  [_shopInfoModel.prodInfo getSizeWithMaxWidth:WIDTH-16 andFontSize:15].height;
        
        if (label_H>label_Height) {
            _footView.size = CGSizeMake(WIDTH, label_H);
        }else{
            _footView.size = CGSizeMake(WIDTH, label_Height);
        }

    }
    
    if ([tag isEqualToString:@"添加购物车"]) {

        NSLog(@"%@",value);
        if ([value[@"ret"] integerValue]== 200) {
            if ([value[@"data"][@"status"] integerValue] == 100) {
                [[iToast makeText:@"已添加到购物车"] show];
            }
            
        }else{
            [[iToast makeText:@"添加失败"] show];
        }
    }
    
}
-(void)downloadData{
    NSString *url = [NSString stringWithFormat:@"%@%@",newPublicURL,@"Prod.GetProdInfo"];
    
    [self.request QZRequest_POST:url parameters:@{@"prod_id":[NSString stringWithFormat:@"%ld",_prodID]} tagNSString:@"商品详情" stopRequest:YES isSerializer:NO isCache:YES];
    
 
}


@end
